﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Kugar.Core.ExtMethod;
using Kugar.MVC.MobileUI.WeUI.Helpers;

#if !NETCOREAPP
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
    using System.Web.Mvc.Html;

#endif

#if NETCOREAPP
using Microsoft.AspNetCore.Mvc.Rendering;
using Kugar.Core.Web;
#endif

namespace Kugar.MVC.MobileUI.WeUI.PageControls.PageForm
{
    public class PageForm_Textarea:PageForm_FieldBase
    {
        private string _placeHolder;
        private string _text = "";
        private int _max = 0;
        private int _row = 3;

        public PageForm_Textarea(ViewContext viewContext) : base(viewContext)
        {
            this.Css("weui-cell__textarea");
        }

        public PageForm_Textarea Placeholder(string text)
        {
            _placeHolder = text;

            return this;
        }

        public PageForm_Textarea Value(string value)
        {
            _text = value.ToStringEx();

            return this;
        }

        public PageForm_Textarea MaxLength(int maxLength)
        {
            _max = maxLength;

            return this;
        }

        public PageForm_Textarea Row(int row)
        {
            _row = row;

            return this;
        }

        public override MvcHtmlString Render()
        {
            ViewContext.Writer.WriteLine($"<div class=\"weui-cells__title\">{getLabel()}</div>");
            
            if (Html.ViewData.ModelState.ContainsKey(getName()))
            {
                _attrs.Class("weui-cell_warn");
            }

            ViewContext.Writer.WriteLine($"<div {_attrs}>");

            ViewContext.Writer.WriteLine("<div class=\"weui-cell__bd\">");

            ViewContext.Writer.WriteLine($"<textarea class=\"weui-textarea\" placeholder=\"{_placeHolder}\" rows=\"{_row}\" name=\"{getName()}\">{_text}</textarea>");

            if (_max>0)
            {
                ViewContext.Writer.WriteLine($"<div class=\"weui-textarea-counter\"><span>{_text.Length}</span>/{_max}</div>");
            }

            ViewContext.Writer.WriteLine("</div> <!-- End cell__bd -->");

            ViewContext.Writer.WriteLine("<div class=\"weui-cell__ft\">");

            ViewContext.Writer.WriteLine("<i class=\"weui-icon-warn\"></i>");

            ViewContext.Writer.WriteLine("</div>");

            ViewContext.Writer.WriteLine("</div> <!-- End  weui-cell-->");

            return MvcHtmlString.Empty;
        }
        
    }

    public class PageForm_TextareaInLine : PageForm_FieldBase
    {
        private string _placeHolder;
        private string _text = "";
        private int _max = 0;
        private int _row = 3;

        public PageForm_TextareaInLine(ViewContext viewContext) : base(viewContext)
        {
            this.Css("weui-cell__textarea");
        }

        public PageForm_TextareaInLine Placeholder(string text)
        {
            _placeHolder = text;

            return this;
        }

        public PageForm_TextareaInLine Value(string value)
        {
            _text = value.ToStringEx();

            return this;
        }

        public PageForm_TextareaInLine MaxLength(int maxLength)
        {
            _max = maxLength;

            return this;
        }

        public PageForm_TextareaInLine Row(int row)
        {
            _row = row;

            return this;
        }

        protected override void RenderBody()
        {
            ViewContext.Writer.WriteLine($"<textarea class=\"weui-textarea\" rows=\"{_row}\" name=\"{getName()}\" placeholder=\"{_placeHolder}\">{_text}</textarea>");

        }
    }
}